package cn.sh.ideal.web.configuration.management.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;

import cn.sh.ideal.web.configuration.management.entity.QueryConditionEntity;
import cn.sh.ideal.web.configuration.management.entity.alarmResponseDto;
import cn.sh.ideal.web.view.display.host.monitor.entity.SearchServerInfoEntity;

@Controller
@PropertySource(value = {"classpath:jdbc.properties"})
@RequestMapping(value = "/data")
public class warningContectsConfigDataController {
	@Bean
	public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
		return new PropertySourcesPlaceholderConfigurer();
	}
	
	private RestTemplate rs = new RestTemplate();
	
	@Value("${server_address}")
	private String serverAddress;
	
	@RequestMapping(value = "/getWarningContectsInfo", method = RequestMethod.POST)
	@ResponseBody
	public String getUserInfo(@RequestBody QueryConditionEntity parameter) {
		String ip = parameter.getIp();
		int page = parameter.getPage();
		int size = parameter.getSize();
		String url = serverAddress+"/getContectsInfo?ip=" + ip
				+"&page=" + page
				+"&pageSize=" + size;

		HttpHeaders headers = new HttpHeaders();
		MediaType type = MediaType.parseMediaType("application/json;charset=UTF-8");
        headers.setContentType(type);
		HttpEntity<String> entity = new HttpEntity<>(new String(), headers);

		ResponseEntity<String> exchange = rs.exchange(url,
				HttpMethod.GET, entity, String.class);

		String result = exchange.getBody();
		//System.out.println(result);
		//ResponseEntity<String> responseEntity = rs.postForEntity(serverAddress+"/getContectsInfo", parameter, String.class);
		//System.out.println(responseEntity.getBody());
		//return responseEntity.getBody();
		return result;
	}
	
	@RequestMapping(value = "/updateWarningContectsInfo", method = RequestMethod.POST)
	@ResponseBody
	public String updateWarningContectsInfo(@RequestBody String entity){
		ArrayList<alarmResponseDto> alarmResponseDaoEntity = (ArrayList<alarmResponseDto>) JSON.parseObject(entity, new TypeReference<ArrayList<alarmResponseDto>>(){});

		for(alarmResponseDto parameter : alarmResponseDaoEntity){
			String ip = parameter.getIp();
			String dangercontact = parameter.getDangercontact();
			Integer dangercontype = parameter.getDangercontype();
			String dangeremail = parameter.getDangeremail();
			String dangerphone = parameter.getDangerphone();
			String id = parameter.getId();
			String seriouscontact = parameter.getSeriouscontact();
			Integer seriouscontype = parameter.getSeriouscontype();
			String seriousemail = parameter.getSeriousemail();
			String seriousphone = parameter.getSeriousphone();
			String warncontact = parameter.getWarncontact();
			Integer warncontype = parameter.getWarncontype();
			String warnemail = parameter.getWarnemail();
			String warnphone = parameter.getWarnphone();
			String url = serverAddress+"/updateContectsInfo?ip=" + ip
					+"&dangercontact=" + dangercontact
					+"&dangercontype=" + dangercontype
					+"&dangeremail=" + dangeremail
					+"&dangerphone=" + dangerphone
					+"&id=" + id
					+"&seriouscontact=" + seriouscontact
					+"&seriouscontype=" + seriouscontype
					+"&seriousemail=" + seriousemail
					+"&seriousphone=" + seriousphone
					+"&warncontact=" + warncontact
					+"&warncontype=" + warncontype
					+"&warnemail=" + warnemail
					+"&warnphone=" + warnphone;
			HttpHeaders headers = new HttpHeaders();
			MediaType type = MediaType.parseMediaType("application/json;charset=UTF-8");
	        headers.setContentType(type);
			HttpEntity<String> httpentity = new HttpEntity<>(new String(), headers);

			ResponseEntity<String> exchange = rs.exchange(url,
					HttpMethod.GET, httpentity, String.class);
		
		}
		return "";
	}
	
	@RequestMapping(value = "/deleteWarningContectsInfo", method = RequestMethod.POST)
	@ResponseBody
	public String deleteWarningContectsInfo(@RequestBody String parameter){
		ArrayList<String> parameterList = (ArrayList<String>) JSON.parseObject(parameter, new TypeReference<ArrayList<String>>(){});
		for(String param : parameterList){
			String url = serverAddress+"/deleteContectsInfo?id=" + param;
			HttpHeaders headers = new HttpHeaders();
			MediaType type = MediaType.parseMediaType("application/json;charset=UTF-8");
	        headers.setContentType(type);
			HttpEntity<String> httpentity = new HttpEntity<>(new String(), headers);

			ResponseEntity<String> exchange = rs.exchange(url,
					HttpMethod.GET, httpentity, String.class);
		}
		
		return "";
	}
	
	@RequestMapping(value = "/insertWarningContectsInfo", method = RequestMethod.POST)
	@ResponseBody
	public String insertWarningContectsInfo(@RequestBody alarmResponseDto dto){
		String ip = dto.getIp();
		String dangercontact = dto.getDangercontact();
		int dangercontype = dto.getDangercontype();
		String dangeremail = dto.getDangeremail();
		String dangerphone = dto.getDangerphone();
		String warncontact = dto.getWarncontact();
		int warncontype = dto.getWarncontype();
		String warnemail = dto.getWarnemail();
		String warnphone = dto.getWarnphone();
		String seriouscontact = dto.getSeriouscontact();
		int seriouscontype = dto.getSeriouscontype();
		String seriousemail = dto.getSeriousemail();
		String seriousphone = dto.getSeriousphone();
		String url = serverAddress+"/insertContectsInfo?ip=" + ip
				+"&dangercontact=" + dangercontact
				+"&dangercontype=" + dangercontype
				+"&dangeremail=" + dangeremail
				+"&dangerphone=" + dangerphone
				+"&seriouscontact=" + seriouscontact
				+"&seriouscontype=" + seriouscontype
				+"&seriousemail=" + seriousemail
				+"&seriousphone=" + seriousphone
				+"&warncontact=" + warncontact
				+"&warncontype=" + warncontype
				+"&warnemail=" + warnemail
				+"&warnphone=" + warnphone;
		HttpHeaders headers = new HttpHeaders();
		MediaType type = MediaType.parseMediaType("application/json;charset=UTF-8");
        headers.setContentType(type);
		HttpEntity<String> httpEntity = new HttpEntity<>(new String(), headers);

		ResponseEntity<String> exchange = rs.exchange(url,
				HttpMethod.GET, httpEntity, String.class);
		System.out.println(exchange.getBody());
		return "";
	}
	
	@RequestMapping(value = "/getIps", method = RequestMethod.POST)
	@ResponseBody
	public String getIps() {
		String url = serverAddress+"/selectIps";

		HttpHeaders headers = new HttpHeaders();
		MediaType type = MediaType.parseMediaType("application/json;charset=UTF-8");
        headers.setContentType(type);
		HttpEntity<String> entity = new HttpEntity<>(new String(), headers);

		ResponseEntity<String> exchange = rs.exchange(url,
				HttpMethod.GET, entity, String.class);
		String result = exchange.getBody();
		return result;
	}
	
	/**
	 * 查询未配置IP地址和systemName
	 * @param: 
	 * @author: zhangyy
	 * @Date:   2018.05.16
	 */
	@RequestMapping(value = "/getIpsAndSystemName", method = RequestMethod.POST)
	@ResponseBody
	public String getIpsAndSystemName() {
		String url = serverAddress+"/selectIpsAndSystemName";

		HttpHeaders headers = new HttpHeaders();
		MediaType type = MediaType.parseMediaType("application/json;charset=UTF-8");
        headers.setContentType(type);
		HttpEntity<String> entity = new HttpEntity<>(new String(), headers);

		ResponseEntity<String> exchange = rs.exchange(url,
				HttpMethod.GET, entity, String.class);
		String result = exchange.getBody();
		return result;
	}
	
	/**
	 * 数据 列表IP地址查询
	 * @param: 
	 * @author: zhongzhaochuan
	 * @Date:   2018.05.25
	 */
	@RequestMapping(value = "/selectIpData", method = RequestMethod.POST)
	@ResponseBody
	public String selectIpData() {
		String url = serverAddress+"/selectIpData";

		HttpHeaders headers = new HttpHeaders();
		MediaType type = MediaType.parseMediaType("application/json;charset=UTF-8");
        headers.setContentType(type);
		HttpEntity<String> entity = new HttpEntity<>(new String(), headers);

		ResponseEntity<String> exchange = rs.exchange(url,
				HttpMethod.GET, entity, String.class);
		String result = exchange.getBody();
		return result;
	}
	
	
	/**
	 * 获取Tree数据
	 * @author: zhongzhaochuan
	 * @Date:   2018.05.25
	 * @param paramter
	 * @return
	 */
	@RequestMapping(value = "/getIpTreeData", method = RequestMethod.POST)
	@ResponseBody
	public String getIpTreeData() {
		
		String url = serverAddress+"/getIpTreeData";

		HttpHeaders headers = new HttpHeaders();
		MediaType type = MediaType
				.parseMediaType("application/json;charset=UTF-8");
		headers.setContentType(type);
		HttpEntity<String> entity = new HttpEntity<>(new String(), headers);

		ResponseEntity<String> exchange = rs.exchange(url, HttpMethod.GET,
				entity, String.class);

		String reult = exchange.getBody();
		return reult;
	}
	
	/**
	 * 获取Tree数据
	 * @author: zhongzhaochuan
	 * @Date:   2018.05.25
	 * @param paramter
	 * @return
	 */
	@RequestMapping(value = "/asyncGetIpTreeData", method = RequestMethod.POST)
	@ResponseBody
	public String asyncGetIpTreeData(@RequestBody SearchServerInfoEntity parameter) {
		String commentId = parameter.getCommentId();
		String hostId = parameter.getHostId();
		String url = serverAddress+"/asyncGetIpTreeData?&commentId="+commentId+"&hostId="+hostId;

		HttpHeaders headers = new HttpHeaders();
		MediaType type = MediaType
				.parseMediaType("application/json;charset=UTF-8");
		headers.setContentType(type);
		HttpEntity<String> entity = new HttpEntity<>(new String(), headers);

		ResponseEntity<String> exchange = rs.exchange(url, HttpMethod.GET,
				entity, String.class);

		String reult = exchange.getBody();
		return reult;
	}
}
